<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use think\facade\View;
use think\Response;

class Message extends Base
{

    public function getOptions(): array
    {
        return [
            'pk'    => 'id',
            'model' => \app\common\model\Message::class,
            'with'  => ['user'],
            'fast'  => [
                'save' => '_save'
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['id', 'ID', fn(Col $col) => $col->width(80)],
                ['uid', '相关会员', fn(Col $col) => $col->minWidth(250)->templet('#_userTpl')],
                ['title', '标题'],
                ['type', '留言类型'],
                ['comment', '描述'],
                ['create_time', '时间', fn(Col $col) => $col->minWidth(140)],
                ['status', '状态', fn(Col $col) => $col->templet('{{ !d.reply_time ? "未回复" : "已回复"}}')],
            ])
            ->addRightButtons(['edit' => fn(Button $btn) => $btn
                    ->class("{{ !d.reply_time ? 'danger' : 'normal' }}")
                    ->title("{{ !d.reply_time ? '回复' : '查看' }}")]
                , 90)
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', fn($s) => $s->relation('\app\common\model\User', 'code|nickname|mobile,id')],
                ['title__comment', '标题/描述筛选', fn(Search $search) => $search->condition('Like')],
                ['create_time', '时间', fn(Search $search) => $search->type('date')]
            ])
            ->fetch();
    }


    public function edit($tpl = 'message/edit')
    {
        $id = $this->request->get('id');
        View::assign([
            'id'   => $id,
            'list' => $this->model::where('id', $id)->order('create_time', 'asc')->select()
        ]);
        return parent::edit($tpl);
    }


}
